<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<form action="/login">
  username <input type="text" name="username"><br>
  password <input type="password" name="password"><br>
  <input type="submit">
</form>
<h3>数据库中登录</h3>
<form action="/loginDb">
  username <input type="text" name="username"><br>
  password <input type="password" name="password"><br>
  <input type="submit">
</form>
<h3>异步登录</h3>
<form>
  username <input type="text" name="username" id="username"><br>
  password <input type="password" name="password" id="password"><br>
  <input type="button" value="异步" onclick="login_async()">
</form>
<script>
  const login_async=async ()=>{
    //首先得到输入的值
    const username=document.querySelector("#username").value;
    const password=document.querySelector("#password").value;
    const data=JSON.stringify({username,password})
    //发送请求
    const res=await fetch('/login_async',{
      method:'post',
      headers:{
        'Content-Type':'application/json'
      },
      body:data
    });
    if(!res.ok){
      alert('login fail');
      return;
    }
    const json=await res.json();
    if(json.code===0){
      alert('login suc');
      window.location.href="/";
    }else{
      alert(json.msg);
    }
    //判断
  }
</script>
</body>
</html>